//
//  FKFastShareCell.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/19.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKFastShareCell.h"
#import "FKFastShareModel.h"

@interface FKFastShareCell ()

@property (nonatomic, weak) UIImageView *avatarView;
@property (nonatomic, weak) UILabel *titleLabel;
@property (nonatomic, weak) UILabel *suggestLabel;
@property (nonatomic, weak) UILabel *contentLabel;
//@property (nonatomic, weak) UIWebView *infoView;

@property (nonatomic, weak) UIView *linkView;
@property (nonatomic, weak) UIImageView *linkIconView;
@property (nonatomic, weak) UILabel *linkTitleLabel;

@property (nonatomic, strong) NSArray *imageViews;
@property (nonatomic, weak) UILabel *timeLabel;
@property (nonatomic, weak) UILabel *shareNumLabel;

@end

@implementation FKFastShareCell

- (void)setupSubviews
{
    [super setupSubviews];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIImageView *avatarView = [[UIImageView alloc] init];
    [self.contentView addSubview:avatarView];
    self.avatarView = avatarView;
    [avatarView fk_viewCornerRadius:fk_adjustW(17.5) borderWidth:0 borderColor:nil];
    
    UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(17) textColor:fkColor333333];
    [self.contentView addSubview:titleLabel];
    self.titleLabel = titleLabel;
    
    UILabel *suggestLabel = [UILabel fk_labelWithFont:fk_adjustFont(13) textColor:fkMainColor];
    [self.contentView addSubview:suggestLabel];
    self.suggestLabel = suggestLabel;
    
    UILabel *contentLabel = [UILabel fk_labelWithFont:fk_adjustFont(16) textColor:fkColor333333];
    [self.contentView addSubview:contentLabel];
    self.contentLabel = contentLabel;
    contentLabel.numberOfLines = 0;
    
    // 如果是链接类型的
    UIView *linkView = [[UIView alloc] init];
    [self.contentView addSubview:linkView];
    self.linkView = linkView;
    linkView.backgroundColor = fkBgColor;
    
    UIImageView *linkIconView = [[UIImageView alloc] init];
    [linkView addSubview:linkIconView];
    self.linkIconView = linkIconView;
    
    UILabel *linkTitleLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333];
    [linkView addSubview:linkTitleLabel];
    self.linkTitleLabel = linkTitleLabel;
    linkTitleLabel.numberOfLines = 3;
    
    
    NSMutableArray *imageViews = [NSMutableArray array];
    for (NSInteger index = 0; index < 9; index++) {
        UIImageView *picView = [[UIImageView alloc] init];
        [self.contentView addSubview:picView];
        [imageViews addObject:picView];
        picView.userInteractionEnabled = true;
        [picView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(picViewDidTap:)]];
    }
    self.imageViews = imageViews;
    
    UIView *bottomView = [[UIView alloc] init];
    [self.contentView addSubview:bottomView];
    
    UILabel *timeLabel = [UILabel fk_labelWithFont:fk_adjustFont(11) textColor:fkColor666666];
    [bottomView addSubview:timeLabel];
    self.timeLabel = timeLabel;
    
    UILabel *shareNumLabel = [UILabel fk_labelWithFont:fk_adjustFont(11) textColor:fkColor666666];
    [bottomView addSubview:shareNumLabel];
    self.shareNumLabel = shareNumLabel;
    
    UIButton *copyBtn = [UIButton fk_btnWithTarget:self action:@selector(copyBtnClicked) font:fk_adjustFont(14) normalTitle:@"复制文案" normalTitleColor:fkColor666666 selectedTitle:nil selectedTitleColor:nil normalImg:[UIImage imageNamed:@"download"] selectedImg:nil bgImg:nil selectedBgImg:nil];
    [self.contentView addSubview:copyBtn];
    copyBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    copyBtn.titleEdgeInsets = UIEdgeInsetsMake(0, fk_adjustW(10), 0, 0);
    
    UILabel *shareTitleLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333 text:@"分享到："];
    [self.contentView addSubview:shareTitleLabel];
    
    UIButton *friendBtn = [UIButton fk_btnWithTarget:self action:@selector(friendBtnClicked) img:[UIImage imageNamed:@"wechat_timeline"]];
    [self.contentView addSubview:friendBtn];
    
    UIButton *wechatBtn = [UIButton fk_btnWithTarget:self action:@selector(wechatBtnClicked) img:[UIImage imageNamed:@"wechat_session"]];
    [self.contentView addSubview:wechatBtn];
    
    UIView *separator = [[UIView alloc] init];
    [bottomView addSubview:separator];
    separator.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    [avatarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(fk_adjustW(35));
        make.left.offset(12);
        make.top.offset(fk_adjustW(10));
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(avatarView);
        make.left.equalTo(avatarView.mas_right).offset(6);
    }];
    [suggestLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(avatarView);
        make.right.offset(-12);
    }];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(avatarView);
        make.right.equalTo(suggestLabel);
        make.top.equalTo(avatarView.mas_bottom).offset(fk_adjustW(12));
    }];
    [linkView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(contentLabel);
        make.height.offset(fk_adjustW(80));
        make.top.equalTo(contentLabel.mas_bottom).offset(fk_adjustW(10));
    }];
    [linkIconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(fk_adjustW(10));
        make.width.height.offset(fk_adjustW(60));
        make.centerY.offset(0);
    }];
    [linkTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(linkIconView.mas_right).offset(8);
        make.right.offset(-8);
        make.top.bottom.offset(0);
    }];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.offset(0);
        make.height.offset(fk_adjustW(65));
    }];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(avatarView);
        make.top.offset(fk_adjustW(10));
    }];
    [copyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(timeLabel);
        make.top.equalTo(timeLabel.mas_bottom).offset(fk_adjustW(15));
        make.width.offset(fk_adjustW(90));
//        make.top.equalTo(timeLabel.mas_bottom);
//        make.bottom.offset(0);
    }];
    [shareNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(suggestLabel);
        make.centerY.equalTo(timeLabel);
    }];
    [wechatBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(shareNumLabel);
        make.centerY.equalTo(copyBtn);
    }];
    [friendBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(wechatBtn.mas_left).offset(fk_adjustW(-20));
        make.centerY.equalTo(copyBtn);
    }];
    [shareTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(friendBtn.mas_left).offset(fk_adjustW(-20));
        make.centerY.equalTo(copyBtn);
    }];
    [separator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(12);
        make.right.offset(-12);
        make.bottom.offset(0);
        make.height.offset(0.8);
    }];
}

- (void)picViewDidTap:(UITapGestureRecognizer *)gesture
{
    [FKLoginTool checkLogin:^{
        UIImageView *picView = (UIImageView *)gesture.view;
        NSInteger index = [self.imageViews indexOfObject:picView];
        if ([self.delegate respondsToSelector:@selector(fastShareCell:didTapImageView:)]) {
            [self.delegate fastShareCell:self didTapImageView:index];
        }
    }];
}

- (void)copyBtnClicked
{
    [FKLoginTool checkLogin:^{
        if ([self.delegate respondsToSelector:@selector(fastShareCellDidClickedCopyBtn:)]) {
            [self.delegate fastShareCellDidClickedCopyBtn:self];
        }
    }];
}

- (void)friendBtnClicked
{
    [FKLoginTool checkLogin:^{
        if ([self.delegate respondsToSelector:@selector(fastShareCellDidClickedWechatTimeline:)]) {
            [self.delegate fastShareCellDidClickedWechatTimeline:self];
        }
    }];
}

- (void)wechatBtnClicked
{
    [FKLoginTool checkLogin:^{
        if ([self.delegate respondsToSelector:@selector(fastShareCellDidClickedWechatSession:)]) {
            [self.delegate fastShareCellDidClickedWechatSession:self];
        }
    }];
}

- (void)setData:(id)data
{
    [super setData:data];
    
    if ([data isKindOfClass:[FKFastShareModel class]]) {
        FKFastShareModel *model = data;
        [self.avatarView sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:kDefaultAvatar];
        self.titleLabel.text = model.app_name;
        self.suggestLabel.text = model.share_time;
        self.contentLabel.text = model.info;
        
        self.linkView.hidden = [model.market_type isEqualToString:@"1"];
        [self.linkIconView sd_setImageWithURL:[NSURL URLWithString:model.img] placeholderImage:kPlaceholder_1x1];
        self.linkTitleLabel.text = model.info;
//        self.infoView.frame = model.infoFrame;
//        self.infoView.backgroundColor = [UIColor redColor];
//        [self.infoView loadHTMLString:model.infoStr baseURL:nil];
        
        for (NSInteger index = 0; index < self.imageViews.count; index++) {
            UIImageView *imageView = self.imageViews[index];
            if (index < model.imageFrames.count) {
                imageView.frame = [model.imageFrames[index] CGRectValue];
                [imageView sd_setImageWithURL:[NSURL URLWithString:model.imgs[index]] placeholderImage:kPlaceholder_1x1];
            } else {
                imageView.frame = CGRectZero;
            }
        }
        self.timeLabel.text = model.publishTime;
        self.shareNumLabel.text = fkFORMAT(@"%@次分享", model.share_num);
    }
}

@end
